<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('设置条件')" :visible.sync="dialogVisible" width="500px" @close='close'>
    <el-form ref="form" :model="record" label-width="150px" :rules="rules">
      <el-form-item :label="$t('IP类型')" prop="ip_type">
        <el-radio v-model="record.ip_type" class="ipv4" label="ipv4">IPv4</el-radio>
        <el-radio v-model="record.ip_type" class="ipv6" label="ipv6">IPv6</el-radio>
      </el-form-item>
      <el-form-item :label="$t('数据方向')" prop="direction">
        <el-radio v-model="record.direction" class="request" label="request">{{$t('请求方向')}}</el-radio>
        <el-radio v-model="record.direction" class="reply" label="reply">{{$t('应答方向')}}</el-radio>
      </el-form-item>
      <el-form-item :label="$t('协议类型')" prop="protocol">
        <el-select v-model="record.protocol" ref="protocol" :placeholder="$t('请选择')" size='small' filterable clearable>
          <el-option v-for="item in protocolList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('源IP')" prop="src_ip">
        <el-input v-emoji v-model="record.src_ip" ref="srcIp" style="width:217px;" size='small'></el-input>
      </el-form-item>
      <el-form-item :label="$t('目的IP')" prop="dst_ip">
        <el-input v-emoji v-model="record.dst_ip" class="dstIp" style="width:217px;" size='small'></el-input>
      </el-form-item>
      <el-form-item :label="$t('源端口')" prop="sport">
        <el-input v-emoji v-model="record.sport" ref="sport" :disabled='!record.protocol' style="width:217px;" size='small'></el-input>
      </el-form-item>
      <el-form-item :label="$t('目的端口')" prop="dport">
        <el-input v-emoji v-model="record.dport" ref="dport" :disabled='!record.protocol' style="width:217px;" size='small'></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { delArray } from '@/api/network/link/show'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'DelShowDialog',
  data () {
    var validateIp = (rule, value, callback) => {
      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((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      // let ipv6 = /^\s*((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}(:|((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(:[0-9A-Fa-f]{1,4})))|(([0-9A-Fa-f]{1,4}:){5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4}){0,4}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))(%.+)?\s*$/
      if (value === '') {
        callback()
      } else if (value !== '' && this.record.ip_type === 'ipv4' && ipv4.test(value)) {
        callback()
      } else if (value !== '' && this.record.ip_type === 'ipv6' && ipv6.test(value)) {
        callback()
      } else {
        callback(new Error(this.$t('IP格式不正确')))
      }
    }
    var validatePost = (rule, value, callback) => {
      if (value) {
        if (isNaN(Number(value))) {
          callback(new Error(this.$t('请输入数字')))
        } else if (Number(value) > 65535 || Number(value) < 0) {
          callback(new Error(this.$t('端口的范围为0~65535')))
        } else if (!this.isInteger(value)) {
          callback(new Error(this.$t('只能输入整数')))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        ip_type: 'ipv4',
        direction: 'request',
        protocol: '',
        src_ip: '',
        dst_ip: '',
        sport: '',
        dport: ''
      },
      recordCopy: {
        ip_type: 'ipv4',
        direction: 'request',
        protocol: '',
        src_ip: '',
        dst_ip: '',
        sport: '',
        dport: ''
      },
      protocolList: ['udp', 'tcp', 'icmp'],
      rules: {
        src_ip: [{ required: false, validator: validateIp, trigger: 'blur' }],
        dst_ip: [{ required: false, validator: validateIp, trigger: 'blur' }],
        sport: [{ required: false, validator: validatePost, trigger: 'blur' }],
        dport: [{ required: false, validator: validatePost, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init () {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['srcIp', 'dstIp', 'sport', 'dport']
        const selectData = ['protocol']
        inputClass(this.$refs, data)
        selectClass(this.$refs, selectData)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          delArray(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
